package com.admin.bombtest

import android.graphics.Color
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.SaveListener
import com.admin.kotlintest.bean.MyUser
import com.admin.kotlintest.R
import com.admin.kotlintest.base.BaseActivity
import com.admin.kotlintest.activity.HomeActivity
import com.admin.kotlintest.config.FixedValue
import com.admin.kotlintest.config.MyApplication
import com.socks.library.KLog

import kotlinx.android.synthetic.main.activity_login.*
import org.jetbrains.anko.startActivity
import org.jetbrains.anko.toast

/**
 * Created by 吴昶 on 2018/3/2.
 */

class LoginActivity : BaseActivity() {

    override fun bindLayout(): Int {
        return R.layout.activity_login
    }

    override fun initData() {
        btn_login.setBackgroundColor(Color.parseColor("#333333"))
        btn_login.setTextColor(Color.WHITE)
        btn_login.textSize = 18f
    }

    override fun initEvent() {
        btn_login.setOnClickListener{
            onViewClicked()
        }
    }

    private fun onViewClicked() {

        var myUser=MyUser()
        myUser.username=edt_user_name.text.toString()
        myUser.setPassword( edt_password.text.toString())
        myUser.login(object :SaveListener<MyUser>(){
            override fun done(p0: MyUser?, p1: BmobException?) {
                p1?:let {
                    if(p0!!.disabled){
                        toast("你已经被禁止登陆，请联系管理员")
                    }else{
                        MyApplication.setSharedPreferencesString(FixedValue.TOKEN,p0.sessionToken)
                        KLog.d(p0.toString())
                        startActivity<HomeActivity>()
                        finish()
                    }
                }

                p1?.let {
                    KLog.d("登陆失败"+p1.message)
                }
            }
        })

    }
}
